﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Ontologies
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class WordFreqInstruction:InstructionBase
    {
        #region src
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _SQL;
        /// <summary></summary>
        public string SQL
        {
            get
            {
                return this._SQL;
            }
            set
            {
                this._SQL = value;
            }
        }

        private string _DocIDField;
        /// <summary></summary>
        public string DocIDField
        {
            get
            {
                return this._DocIDField;
            }
            set
            {
                this._DocIDField = value;
            }
        }

        private string _TextField;
        /// <summary></summary>
        public string TextField
        {
            get
            {
                return this._TextField;
            }
            set
            {
                this._TextField = value;
            }
        }

        private string _YearField;
        /// <summary></summary>
        public string YearField
        {
            get
            {
                return this._YearField;
            }
            set
            {
                this._YearField = value;
            }
        }
        #endregion

        private string _OutputConnStr;
        /// <summary></summary>
        public string OutputConnStr
        {
            get
            {
                return this._OutputConnStr;
            }
            set
            {
                this._OutputConnStr = value;
            }
        }

        #region terms 
        private string _TermTable;
        /// <summary></summary>
        public string TermTable
        {
            get
            {
                return this._TermTable;
            }
            set
            {
                this._TermTable = value;
            }
        }

        private string _TermIDField;
        /// <summary></summary>
        public string TermIDField
        {
            get
            {
                return this._TermIDField;
            }
            set
            {
                this._TermIDField = value;
            }
        }

        private string _PhraseField;
        /// <summary></summary>
        public string PhraseField
        {
            get
            {
                return this._PhraseField;
            }
            set
            {
                this._PhraseField = value;
            }
        }

        private string _RootTermIDField;
        /// <summary></summary>
        public string RootTermIDField
        {
            get
            {
                return this._RootTermIDField;
            }
            set
            {
                this._RootTermIDField = value;
            }
        }
        #endregion

        #region doc terms
        private string _DocTermTable;
        /// <summary></summary>
        public string DocTermTable
        {
            get
            {
                return this._DocTermTable;
            }
            set
            {
                this._DocTermTable = value;
            }
        }

        private string _TermIDField2;
        /// <summary></summary>
        public string TermIDField2
        {
            get
            {
                return this._TermIDField2;
            }
            set
            {
                this._TermIDField2 = value;
            }
        }

        private string _DocIDField2;
        /// <summary></summary>
        public string DocIDField2
        {
            get
            {
                return this._DocIDField2;
            }
            set
            {
                this._DocIDField2 = value;
            }
        }

        private string _TermFreqField;
        /// <summary></summary>
        public string TermFreqField
        {
            get
            {
                return this._TermFreqField;
            }
            set
            {
                this._TermFreqField = value;
            }
        }
        #endregion

        #region neighbors
        private string _NeighborTable;
        /// <summary></summary>
        public string NeighborTable
        {
            get
            {
                return this._NeighborTable;
            }
            set
            {
                this._NeighborTable = value;
            }
        }

        private string _LeftTermIDField;
        /// <summary></summary>
        public string LeftTermIDField
        {
            get
            {
                return this._LeftTermIDField;
            }
            set
            {
                this._LeftTermIDField = value;
            }
        }

        private string _RightTermIDField;
        /// <summary></summary>
        public string RightTermIDField
        {
            get
            {
                return this._RightTermIDField;
            }
            set
            {
                this._RightTermIDField = value;
            }
        }

        private string _DocContainingNeighborField;
        /// <summary></summary>
        public string DocContainingNeighborField
        {
            get
            {
                return this._DocContainingNeighborField;
            }
            set
            {
                this._DocContainingNeighborField = value;
            }
        }

        private string _NeighborTotalOccuranceField;
        /// <summary></summary>
        public string NeighborTotalOccuranceField
        {
            get
            {
                return this._NeighborTotalOccuranceField;
            }
            set
            {
                this._NeighborTotalOccuranceField = value;
            }
        }
        #endregion

        #region trends
        private string _TrendTable;
        /// <summary></summary>
        public string TrendTable
        {
            get
            {
                return this._TrendTable;
            }
            set
            {
                this._TrendTable = value;
            }
        }

        private string _TermIDField3;
        /// <summary></summary>
        public string TermIDField3
        {
            get
            {
                return this._TermIDField3;
            }
            set
            {
                this._TermIDField3 = value;
            }
        }

        private string _TermYearField;
        /// <summary></summary>
        public string TermYearField
        {
            get
            {
                return this._TermYearField;
            }
            set
            {
                this._TermYearField = value;
            }
        }

        private string _SumTermFreqField;
        /// <summary></summary>
        public string SumTermFreqField
        {
            get
            {
                return this._SumTermFreqField;
            }
            set
            {
                this._SumTermFreqField = value;
            }
        }

        private string _SumDocFreqField;
        /// <summary></summary>
        public string SumDocFreqField
        {
            get
            {
                return this._SumDocFreqField;
            }
            set
            {
                this._SumDocFreqField = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public WordFreqInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._SQL = string.Empty;
            this._DocIDField = string.Empty;
            this._TextField = string.Empty;
            this._YearField = string.Empty;

            this._OutputConnStr = string.Empty;
            this._TermTable = string.Empty;
            this._TermIDField = string.Empty;
            this._PhraseField = string.Empty;
            this._RootTermIDField = string.Empty;

            this._DocTermTable = string.Empty;
            this._TermIDField2 = string.Empty;
            this._DocIDField = string.Empty;
            this._TermFreqField = string.Empty;

            this._NeighborTable = string.Empty;
            this._LeftTermIDField = string.Empty;
            this._RightTermIDField = string.Empty;
            this._NeighborTotalOccuranceField = string.Empty;
            this._DocContainingNeighborField = string.Empty;

            this._TrendTable = string.Empty;
            this._TermIDField3 = string.Empty;
            this._TermYearField = string.Empty;
            this._SumTermFreqField = string.Empty;
            this._SumDocFreqField = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        public WordFreqInstruction(Guid componentID, Guid jobTicketID,
            string connStr, string sql, string docIDField,
            string textField, string yearField,
            string outputConnStr, string outputTable, string termIDField,
            string phraseField, string rootTermIDField, 
            string docTermTable, string termIDFieldInDocTermTable,
            string docIDFieldInDocTermTable, 
            string termFreqFieldInDocTerm,
            string neighborTable, string leftTermIDField, 
            string rightTermIDField,
            string neighborOccuranceField, 
            string docsContainingNeighborField,
            string trendTable, string termIDFieldInTrend, 
            string yearFieldInTrend,
            string sumTermFreqField, string sumDocFreqField) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._SQL = sql;
            this._DocIDField = docIDField;
            this._TextField = textField;
            this._YearField = yearField;
            this._OutputConnStr = outputConnStr;

            this._TermTable = outputTable;
            this._TermIDField = termIDField;
            this._PhraseField = phraseField;
            this._RootTermIDField = rootTermIDField;

            this._DocTermTable = docTermTable;
            this._TermIDField2 = termIDFieldInDocTermTable;
            this._DocIDField = docIDFieldInDocTermTable;
            this._TermFreqField = termFreqFieldInDocTerm;

            this._NeighborTable = neighborTable;
            this._LeftTermIDField = leftTermIDField;
            this._RightTermIDField = rightTermIDField;
            this._NeighborTotalOccuranceField = neighborOccuranceField;
            this._DocContainingNeighborField = docsContainingNeighborField;

            this._TrendTable = trendTable;
            this._TermIDField3 = termIDFieldInTrend;
            this._TermYearField = yearFieldInTrend;
            this._SumTermFreqField = sumTermFreqField;
            this._SumDocFreqField = sumDocFreqField;
            
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SQL", this.SQL);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocIDField",this.DocIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TextField", this.TextField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "YearField", this.YearField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputConnStr", this.OutputConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputTable", this.TermTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TermIDField", this.TermIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PhraseField", this.PhraseField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RootTermIDField", this.RootTermIDField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocTermTable", this.DocTermTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TermIDFieldInDocTerm", this.TermIDField2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocIDFieldInDocTerm", this.DocIDField2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TermFreqField", this.TermFreqField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NeighborTable", this.NeighborTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LeftTermIDField", this.LeftTermIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RightTermIDField", this.RightTermIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NeighborOccuranceField", this.NeighborTotalOccuranceField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocsContainingNeighborField", this.DocContainingNeighborField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TrendTable", this.TrendTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TermIDFieldInTrend", this.TermIDField3);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "YearFieldInTrend", this.TermYearField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SumTermFreqField", this.SumTermFreqField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SumDocFreqField", this.SumDocFreqField);
            
            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                WordFreqInstruction instruction = new WordFreqInstruction();
                instruction.OwnerComponentID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "ComponentID", Guid.NewGuid().ToString()));
                instruction.OwnerWorkerInstanceID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "WorkerID", Guid.NewGuid().ToString()));
                instruction.SubmitTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "SubmitTime", TimeUtil.DbMinTime.ToString()));
                instruction.StartTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "StartTime", TimeUtil.DbMinTime.ToString()));

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.SQL = XmlDataUtil.GetAttributeValue(xNode, "SQL", "");
                instruction.DocIDField = XmlDataUtil.GetAttributeValue(xNode, "DocIDField", "");
                instruction.TextField = XmlDataUtil.GetAttributeValue(xNode, "TextField", "");
                instruction.YearField = XmlDataUtil.GetAttributeValue(xNode, "YearField", "");

                instruction.OutputConnStr = XmlDataUtil.GetAttributeValue(xNode, "OutputConnStr", "");
                instruction.TermTable = XmlDataUtil.GetAttributeValue(xNode, "OutputTable", "");
                instruction.TermIDField = XmlDataUtil.GetAttributeValue(xNode, "TermIDField", "");
                instruction.PhraseField = XmlDataUtil.GetAttributeValue(xNode, "PhraseField", "");
                instruction.RootTermIDField = XmlDataUtil.GetAttributeValue(xNode, "RootTermIDField", "");

                instruction.DocTermTable = XmlDataUtil.GetAttributeValue(xNode, "DocTermTable", "");
                instruction.TermIDField2 = XmlDataUtil.GetAttributeValue(xNode, "TermIDFieldInDocTerm", "");
                instruction.DocIDField2 = XmlDataUtil.GetAttributeValue(xNode, "DocIDFieldInDocTerm", "");
                instruction.TermFreqField = XmlDataUtil.GetAttributeValue(xNode, "TermFreqField", "");

                instruction.NeighborTable = XmlDataUtil.GetAttributeValue(xNode, "NeighborTable", "");
                instruction.LeftTermIDField = XmlDataUtil.GetAttributeValue(xNode, "LeftTermIDField", "");
                instruction.RightTermIDField = XmlDataUtil.GetAttributeValue(xNode, "RightTermIDField", "");
                instruction.NeighborTotalOccuranceField =
                    XmlDataUtil.GetAttributeValue(xNode, "NeighborOccuranceField", "");
                instruction.DocContainingNeighborField =
                    XmlDataUtil.GetAttributeValue(xNode, "DocsContainingNeighborField", "");

                instruction.TrendTable = XmlDataUtil.GetAttributeValue(xNode, "TrendTable", "");
                instruction.TermIDField3 = XmlDataUtil.GetAttributeValue(xNode, "TermIDFieldInTrend", "");
                instruction.TermYearField = XmlDataUtil.GetAttributeValue(xNode, "YearFieldInTrend", "");
                instruction.SumTermFreqField = XmlDataUtil.GetAttributeValue(xNode, "SumTermFreqField", "");
                instruction.SumDocFreqField = XmlDataUtil.GetAttributeValue(xNode, "SumDocFreqField", "");
                
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
